﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ExcelAutomator
{
	public static class ReflectionHelper
	{
		public static IEnumerable<PropertyInfo> GetAllProperties(Type type)
		{
			var result = new List<PropertyInfo>();

			foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
			{
				if (propertyInfo.PropertyType.IsGenericType)
				{
					var test = propertyInfo.PropertyType.GetGenericTypeDefinition();

					if (typeof(IEnumerable).IsAssignableFrom(test))
					{
						continue;
					}
				}

				if (!propertyInfo.PropertyType.IsValueType && 
					propertyInfo.PropertyType != typeof(object) && 
					propertyInfo.PropertyType != typeof(string) && 
					propertyInfo.PropertyType != typeof(Delegate))
				{
					result.AddRange(GetAllProperties(propertyInfo.PropertyType));
				}
				else
				{
					result.Add(propertyInfo);
				}
			}
			return result.Distinct();
		}
	}
}
